/*
	Visp is a visual performance application (VJ app) built on Adobe's AIR runtime framework.
	Copyright (C) 2007 Mike Creighton
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
	
	Mike Creighton can be found at http://www.mikecreighton.com	
*/
package com.visp.transitions
{
	import flash.events.IEventDispatcher;
	import flash.geom.ColorTransform;
	import flash.display.BitmapData;
	import flash.utils.*;
	import com.visp.render.Layer;
	import flash.geom.Point;
	import flash.filters.BlurFilter;
	import flash.filters.ColorMatrixFilter;
	import com.visp.constants.POINT;
	

	public class BlurredCrossfadeTransition extends CrossfadeTransition
	{
		private static const MAX_BLUR : int = 20;
				
		private var _activeBlurBmp : BitmapData;
		private var _queuedBlurBmp : BitmapData;
		private var _activeBlur : BlurFilter;
		private var _queuedBlur : BlurFilter;
		private var _queuedBlurAmt : Number;
		private var _activeBlurAmt : Number;
		
		public function BlurredCrossfadeTransition(target:IEventDispatcher=null)
		{
			super(target);
			this._activeBlurBmp = this._output.clone();
			this._queuedBlurBmp = this._output.clone();
		}
		
		override public function start(queued : BitmapData, active : BitmapData) : void 
		{
			super.start(queued, active);
			
			this._queuedBlurAmt = MAX_BLUR;
			this._activeBlurAmt = 0;
			this._queuedBlur = new BlurFilter(this._queuedBlurAmt, this._queuedBlurAmt, 1);
			this._activeBlur = new BlurFilter(this._activeBlurAmt, this._activeBlurAmt, 1);
		}
		
		override public function render(queued:BitmapData, active:BitmapData) : BitmapData
		{
						
			var dur : Number = TransitionManager.getInstance().getDuration();
			var halfDur : Number = dur / 2;
			var quarterDur : Number = dur / 4;
			var threeQuarterDur : Number = quarterDur * 3;
			
			var elapsed : int = getTimer() - this._startTime;
			
			// First we'll deal with the blurs calculation.
			if(elapsed < quarterDur){
				this._queuedBlurAmt = MAX_BLUR;
			} else if (elapsed > threeQuarterDur){
				this._queuedBlurAmt = 0;
			} else {
				this._queuedBlurAmt = MAX_BLUR - (((elapsed - quarterDur) / halfDur) * MAX_BLUR);
			}
			
			// Now the active blur
			if(elapsed < quarterDur){
				this._activeBlurAmt = 0;
			} else if (elapsed > threeQuarterDur){
				this._activeBlurAmt = MAX_BLUR;
			} else {
				this._activeBlurAmt = ((elapsed - quarterDur) / halfDur) * MAX_BLUR;
			}
			
			this._queuedBlur.blurX = this._queuedBlur.blurY = this._queuedBlurAmt;
			this._activeBlur.blurX = this._activeBlur.blurY = this._activeBlurAmt;
			
			// Apply the blurs
			if(_activeBlurAmt > 0){
				this._activeBlurBmp.copyPixels(active, active.rect, POINT);
				active.applyFilter(this._activeBlurBmp, this._activeBlurBmp.rect, POINT, this._activeBlur);
			}
			
			if(_queuedBlurAmt > 0){
				this._queuedBlurBmp.copyPixels(queued, queued.rect, POINT);
				queued.applyFilter(this._queuedBlurBmp, this._queuedBlurBmp.rect, POINT, this._queuedBlur);
			}
			
			return super.render(queued, active);
		}
		
		override public function destroy():void
		{
			super.destroy();
			this._activeBlurBmp.dispose();
			this._activeBlurBmp = null;
			this._queuedBlurBmp.dispose();
			this._queuedBlurBmp = null;
		}
	}
}